package com.njym.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.njym.annotation.PermInfo;
import com.njym.entity.SysUser;
import com.njym.service.ReconChannelService;
import com.njym.service.ReconDetailService;
import com.njym.util.PageUtils;
import com.njym.vo.Json;
import com.njym.vo.ReconChannelVo;
import com.njym.vo.ReconDetailVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: ReconciliationsController
 * @description:
 * @author: yh
 * @create: 2018-09-12 13:57
 **/
@PermInfo(value = "对账记录接口")
@RestController
@RequestMapping("/ReconDetail")
public class ReconDetailController {

    @Autowired
    ReconDetailService reconDetailService;

    private static final Logger log = LoggerFactory.getLogger(ReconDetailController.class);

    @PostMapping("/listdetail")
    public Json listDetail(@RequestBody String body) {
        String oper = "list detail";
        log.info("{}, body: {}", oper, body);

        JSONObject json = JSON.parseObject(body);
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        String orgId = user.getOrgId();
        if (orgId.equals("0")) {
            orgId = null;
        }

        Date tradeDate = json.getDate("tradeDate");
        Date accountDate = json.getDate("accountDate");
        BigDecimal PlatFee = json.getBigDecimal("PlatFee");
        BigDecimal OrgFee = json.getBigDecimal("OrgFee");

        Object timeInterval = json.get("timeInterval");
        Date start = null;
        Date end = null;
        if (timeInterval != null && timeInterval.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeInterval;
            start = timeIntervalarr.getTimestamp(0);
            end = timeIntervalarr.getTimestamp(1);
        }
        Page<ReconDetailVo> page = reconDetailService.getDetail(PageUtils.getPageParam(json),orgId,tradeDate,accountDate,PlatFee,OrgFee,start,end);
        System.out.print(page);
        return Json.succ(oper).data("page", page);
    }
}
